﻿@typeparam TItem
<div class="dropdown">
    <button class="btn btn-primary dropdown-toggle mr-4" data-toggle="dropdown" type="button" @onclick="e => this.show=!this.show "
            aria-haspopup="true" aria-expanded="false">
        @Tip
    </button>
    <CascadingValue name="Dropdown" Value="@this">
        <div class="dropdown-menu @(show? "show":"")">
            @ChildContent
        </div>
    </CascadingValue>
</div>

@code {
    [Parameter]
    public RenderFragment InitialTip { get; set; }
    [Parameter]
    public RenderFragment ChildContent { get; set; }
    [Parameter]
    public EventCallback<TItem> OnSelected { get; set; }

    private bool show = false;
    private RenderFragment Tip;

    protected override void OnInitialized() { this.Tip = InitialTip; }
    public async Task HandleSelect(TItem item, RenderFragment<TItem> contentFragment)
    {
        this.Tip = contentFragment.Invoke(item);
        this.show = false;
        StateHasChanged();
        await this.OnSelected.InvokeAsync(item);
    }
}